/*
 * @Author: wangming
 * @Date: 2022-06-28 14:12:19
 * @LastEditors: wangming
 * @LastEditTime: 2022-06-28 14:12:20
 * @FilePath: /hanma-application-designer-fed/ver.build.js
 * @Description: 
 */
const fs = require("fs-extra");
const chalk = require("chalk");
let child_process = require("child_process");

let moment = null;
try {
  moment = require("dayjs");
} catch (err) {
  console.log(chalk.red(err.message + "\r\n"));
}

if (moment === null) {
  try {
    moment = require("moment");
  } catch (err) {
    console.log(chalk.red(err.message + "\r\n"));
  }
}

const verPath = "public/ver.build.log";
const logLen = 20;

const time =
  "Build Time:" +
  "\r\n" +
  (moment ? moment(Date.now()).format("YYYY-MM-DD HH:mm:ss") : new Date());
const logTip = `Git Logs(Latest ${logLen}):`;
const logs = child_process.execSync(`git log -${logLen} --oneline --no-merges`);

const content = time + "\r\n\r\n" + logTip + "\r\n" + logs;

fs.ensureFile(verPath).then(() => {
  fs.writeFileSync(verPath, content);
  console.log(chalk.green(content));
});
